package com.example.com.skills_utf8;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.example.com.skills_utf8.fragment32_1.Adapter;
import com.example.com.skills_utf8.fragment32_1.ChildBean;
import com.example.com.skills_utf8.fragment32_1.GroupViewHolder;
import com.example.com.skills_utf8.fragment32_1.ParentBean;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;







public class CopyOfExpandableListView33Activity extends Activity {

	CopyOfExpandableListView33Activity activity = this;
	
	
	
	
	class ChildBean
	{
		 String content;
	};
	
	class ParentBean
	{
		ArrayList<ChildBean> array_content = new ArrayList<ChildBean>();
		String title;
	};
	
	ArrayList<ParentBean>    bean = new ArrayList<ParentBean>();;   
	Adapter adapter;
	private ExpandableListView listview;
	
	
	
	
	void add_data(){
		
		
		for(int i =0; i<2; i++){
			ParentBean parent = new ParentBean();
			parent.title=("标题" + i);
			parent.array_content = new ArrayList<ChildBean>();
			for(int j =0; j<3; j++){
				ChildBean child = new ChildBean();
				child.content=("第"+j);
				parent.array_content.add(child);
			}
			
			bean.add(parent);
		}
		
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_32);
		
		
		 listview = (ExpandableListView)findViewById(R.id.expandable_list_view);
		
		
	    add_data(); 
        
			adapter = new Adapter( );
			listview.setAdapter(adapter);
			
		Button btn_add_data = (Button)findViewById(R.id.btn_add_data);
		btn_add_data.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		
				
				adapter.addData("标题", "子项");
				
			}
		});
		
		
		
		
	}
	
	

	
	

	// http://blog.csdn.net/arylo/article/details/8711769
	// http://www.cnblogs.com/xuewater/archive/2012/08/09/2631017.html
	class Adapter extends BaseExpandableListAdapter
	{
		
	    
		/**
		 * 添加一个数据
		 * @param groupdata
		 * @param childdata
		 */
		public void addData(String groupdata, String childdata) {
				
				ChildBean child = new ChildBean();
				child.content=childdata; 
		           
	        	ParentBean parent = new ParentBean();  
	        	parent.title = groupdata;
	            parent.array_content.add(child); 
	               
	            bean.add(parent);   
	            
	            notifyDataSetChanged();
				
		}

		

		///*
		public  TextView getGenericView(String string)   
	    {   
	        // Layout parameters for the ExpandableListView    
	        AbsListView.LayoutParams layoutParams = new  AbsListView.LayoutParams(   
	                ViewGroup.LayoutParams.FILL_PARENT, 64 );   
	        TextView text = new  TextView(activity);   
	        text.setLayoutParams(layoutParams);   
	        // Center the text vertically    
	        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);   
	        // Set the text starting position    
	        text.setPadding(60 , 0 , 0 , 0 );   
	        text.setText(string);   
	        return  text;   
	    } 
		//*/
		

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return  bean.size(); 
		}

		@Override
		public int getChildrenCount(int groupPosition) { 
			// TODO Auto-generated method stub
			return  bean.get(groupPosition).array_content.size();  
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			 return  bean.get(groupPosition); 
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return  bean.get(groupPosition).array_content.get(childPosition);  
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			 return  childPosition;  
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
		
			
			System.out.println( "getGroupView===================" );
			
			
			final GroupViewHolder ViewHolder;
			
			if(convertView == null){
				convertView = activity.getLayoutInflater().inflate(R.layout.adapter_32_1, null);
				
				ViewHolder = new GroupViewHolder();
				ViewHolder.cb_select = (CheckBox)convertView.findViewById(R.id.cb_select);
				
				convertView.setTag(ViewHolder);
			}
			else{
				ViewHolder = (GroupViewHolder)convertView.getTag();
			}
			
			
			
			return convertView;
			
			
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			System.out.println( "getChildView=================" );
			
			
			String string = bean.get(groupPosition).array_content.get(childPosition).content;
	        return  getGenericView(string);
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean areAllItemsEnabled() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isEmpty() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onGroupExpanded(int groupPosition) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGroupCollapsed(int groupPosition) {
			// TODO Auto-generated method stub
			
		}

		
		
		@Override
		public long getCombinedChildId(long groupId, long childId) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		@Override
		public long getCombinedGroupId(long groupId) {
			// TODO Auto-generated method stub
			return 0;
		}
		
	};
	
	
	class GroupViewHolder
	{
		public CheckBox cb_select;
	};
	
	
	class childViewHolder
	{
		
	};
	
	
	
	
}
